package ar.edu.sccs.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ar.edu.sccs.dao.CaseDTO;
import ar.edu.sccs.dao.RepositoryDTO;
import ar.edu.sccs.entities.Case;
import ar.edu.sccs.model.loader.dto.DataSCM;
import ar.edu.sccs.util.svn.DisplayFile;
import ar.edu.sccs.util.svn.DisplayRepositoryTree;

public class SvnHelper {

	private static Map<String, String> codeMap = new HashMap<String, String>();

	public static String recoverCode(CaseDTO caseToRecover, RepositoryDTO repository) {
		String url = repository.getRepository_protocol() + repository.getRepository_server() + repository.getRepository_name();
		String name = repository.getRepository_user();
		String password = repository.getRepository_password();
		String filePath = caseToRecover.getScmUrl();

		String hashKey = url + "/" + filePath;
		if (codeMap.containsKey(hashKey))
			return codeMap.get(hashKey);

		String code = DisplayFile.displayFile(url, filePath, name, password);
		codeMap.put(hashKey, code);
		return code;
	}

	public static String recoverCode(Case caseToRecover, RepositoryDTO repository) {
		String url = repository.getRepository_protocol() + repository.getRepository_server() + repository.getRepository_name();
		String name = repository.getRepository_user();
		String password = repository.getRepository_password();
		String filePath = caseToRecover.getScmUrl();

		String hashKey = url + "/" + filePath;
		if (codeMap.containsKey(hashKey))
			return codeMap.get(hashKey);

		String code = DisplayFile.displayFile(url, filePath, name, password);
		codeMap.put(hashKey, code);
		return code;
	}

	public static List<String> recoverFilesProject(DataSCM dataRepository) {
		String url = dataRepository.getRepositoryServer() + dataRepository.getRepositoryUrl();
		String name = dataRepository.getRepositoryUsr();
		String password = dataRepository.getRepositoryPwd();
		return DisplayRepositoryTree.displayRepositoryTree(url, name, password);
    }
}
